﻿using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;


using System.Windows.Forms;
using ITAP.ShprossesBuilder.ShprossesPropertyGrid.ComponentsProxy;
using ITAP.ShprossesBuilder.Calculations.CoordinateTransformations;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.Shprosses.CustomPropertyGrid.ComponentsProxy
{
    public class ShprossProxy : Proxy
    {
        private ICoordinateTransformer coorditateTransformer;
        private Shpross shpross;
        public ShprossProxy(Shpross shpross, MainControl mainControl) : base(mainControl)
        {
            this.shpross = shpross;
            this.coorditateTransformer = mainControl.ModelControl.CoordinateTransformer;
        }

        //[Category("Общие свойства"), DisplayName("Длина с отступами")]
        //public double LengthWithSpacerIndents
        //{
        //    get
        //    {
        //        return shpross.LengthWithSpacerIndents;
        //    }
        //}

        [Category("Общие свойства"), DisplayName("Расстояние между концами"), ReadOnly(true)]
        public double DistanceBetweenEnds
        {
            get
            {
                return Math.Round(ModelCalculations.GetDistance(shpross.StartPoint, shpross.EndPoint), 1, MidpointRounding.ToEven);
            }
        }


        [Category("Общие свойства"), DisplayName("Правильная арка")]
        public bool IsHalfRound
        {
            get
            {
                return shpross.IsHalfRound;
            }
            set
            {
                if (shpross.Height == 0)
                {
                    MessageBox.Show("Для задания правильной арки профиль должен быть выгнут.");
                    return;
                }
                double oldShprossHeight = shpross.Height;
                
                shpross.IsHalfRound = value;

                if (!shpross.Parent.BoundingBox.Contains(shpross.BoundingBox))
                {
                    MessageBox.Show("Необходимо, чтобы профиль помещался в размеры модели.");
                    shpross.IsHalfRound = !value;
                    shpross.Height = oldShprossHeight;
                }
            }
        }

        [Category("Общие свойства"), DisplayName("Артикул")]
        [ReadOnly(true)]
        public string Name
        {
            get { return shpross.Article; }
            set { shpross.Article = value; }
        }
        [Category("Положение"), DisplayName("X")]
        public double X
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.Position);
                return Math.Round(point.X, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint point = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(value, 0));
                shpross.MoveTo(new ModelPoint(point.X, shpross.Position.Y));
            }
        }
        [Category("Положение"), DisplayName("Y")]
        public double Y
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.Position);
                return Math.Round(point.Y, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint point = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(0, value));                
                shpross.MoveTo(new ModelPoint(shpross.Position.X, point.Y));
            }
        }

        [Category("Общие свойства"), DisplayName("Ширина"), ReadOnly(true)]
        public double Width
        {
            get
            {                
                return Math.Round(shpross.Width, 1, MidpointRounding.ToEven);
            }
        }


        [Category("Положение начала"), DisplayName("X")]
        public double StartX
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.StartPoint);
                return Math.Round(point.X, 1, MidpointRounding.ToEven);
            }
            set
            {                   
                this.ExecutePropertyValueChangingCommand();
                ModelPoint newPoint = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(value, 0));
                ModelPoint oldStartPoint = shpross.StartPoint;
                shpross.MoveStartPointTo(new ModelPoint(newPoint.X, oldStartPoint.Y));
                

            }
        }

        [Category("Положение начала"), DisplayName("Y")]
        public double StartY
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.StartPoint);
                return Math.Round(point.Y, 1, MidpointRounding.ToEven);
            }
            set
            {
                ModelPoint newPoint = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(0, value));
                ModelPoint oldStartPoint = shpross.StartPoint;
                shpross.MoveStartPointTo(new ModelPoint(oldStartPoint.X, newPoint.Y));
            }
        }

        [Category("Положение конца"), DisplayName("X")]
        public double EndX
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.EndPoint);
                return Math.Round(point.X, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint newPoint = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(value, 0));
                ModelPoint oldEndPoint = shpross.EndPoint;
                shpross.MoveEndPointTo(new ModelPoint(newPoint.X, oldEndPoint.Y));
            }
        }

        [Category("Положение конца"), DisplayName("Y")]
        public double EndY
        {
            get
            {
                ModelPoint point = coorditateTransformer.TransformTo(mainControl.ModelContainer, shpross.EndPoint);
                return Math.Round(point.Y, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint newPoint = coorditateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(0, value));
                ModelPoint oldEndPoint = shpross.EndPoint;
                shpross.MoveEndPointTo(new ModelPoint(oldEndPoint.X, newPoint.Y));
            }
        }


        [Category("Общие свойства"), DisplayName("Длина профиля")]
        public double Length
        {
            get
            {
                
                return Math.Round(shpross.Length, 1, MidpointRounding.ToEven);
            }
            set
            {
                double oldLength = shpross.Length;
                shpross.Length = value;
                if (!shpross.Parent.BoundingBox.Contains(shpross.BoundingBox))
                    shpross.Length = oldLength;
            }
        }

        [Category("Положение"), DisplayName("Высота арки")]
        public double Height
        {
            get
            {
                return -Math.Round(shpross.Height, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                shpross.Height = -value;
            }
        }

    }
}
